package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.service.MemberSearchHistoryService;
import com.cskaoyan.market.db.domain.MarketSearchHistory;
import com.cskaoyan.market.db.domain.MarketSearchHistoryExample;
import com.cskaoyan.market.db.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author TangHonghao
 * @since 2023/03/21 09:59
 */
public class MemberSearchHistoryServiceImpl implements MemberSearchHistoryService {

    @Override
    public List<MarketSearchHistory> getSearchHistoryAll() {
        SqlSession session = MyBatisUtil.getSession();
        MarketSearchHistoryMapper searchHistoryMapper = session.getMapper(MarketSearchHistoryMapper.class);
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        List<MarketSearchHistory> marketSearchHistories =
                searchHistoryMapper.selectByExample(marketSearchHistoryExample);
        return marketSearchHistories;
    }

    @Override
    public List<MarketSearchHistory> getSearchHistoryByUserId(Integer uId) {
        SqlSession session = MyBatisUtil.getSession();
        MarketSearchHistoryMapper searchHistoryMapper = session.getMapper(MarketSearchHistoryMapper.class);
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.or().andUserIdEqualTo(uId);
        List<MarketSearchHistory> marketSearchHistories = searchHistoryMapper.selectByExample(marketSearchHistoryExample);
        session.commit();
        session.close();
        return marketSearchHistories;
    }

    @Override
    public List<MarketSearchHistory> getSearchHistoryByKeyword(String keyword) {
        SqlSession session = MyBatisUtil.getSession();
        MarketSearchHistoryMapper searchHistoryMapper = session.getMapper(MarketSearchHistoryMapper.class);
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.or().andKeywordLike("%"+keyword+"%");
        List<MarketSearchHistory> marketSearchHistories = searchHistoryMapper.selectByExample(marketSearchHistoryExample);
        session.commit();
        session.close();
        return marketSearchHistories;
    }
}
